{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "03582102",
   "metadata": {},
   "source": [
    "# 本周作业\n",
    "* 1.请同学们利用python函数的方法，封装 face++ 人脸检测，人脸分析和人脸对比\n",
    "* 2.请同学们调用face++ 人脸搜索API接口和人脸库API组\n",
    "* 3.尝试应用 上述 2 ，设计宿舍人脸识别系统小应用（微信小程序尝试）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a05df1e",
   "metadata": {},
   "source": [
    "## 请同学们利用python函数的方法，封装 face++ 人脸检测，人脸分析和人脸对比\n",
    "### python函数的方法，封装face++ 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2d6ee5bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(API_key,API_secret,image_url,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++ 人脸检测接口函数(图片链接)\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes,\n",
    "        'face_rectangle':face_rectangle,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max   \n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "501e0933",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647587745,b11d03ea-1ad5-45e2-8715-4f462474690b',\n",
       " 'time_used': 195,\n",
       " 'faces': [{'face_token': 'ef118926843f1fff2a46b5099c3ab40c',\n",
       "   'face_rectangle': {'top': 159, 'left': 175, 'width': 206, 'height': 206}}],\n",
       " 'image_id': 'NjVkgelKFZk8jOCBzxQzTQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect('jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf','N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR','https://img2.baidu.com/it/u=1460619778,3149038650&fm=253&fmt=auto&app=138&f=PNG?w=500&h=667')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5430a230",
   "metadata": {},
   "source": [
    "### Python函数的方法，封装face++ 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a083268a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_analyze(API_key,API_secret,face_tokens,landmark=1,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++ 人脸分析接口函数(图片链接)\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'face_tokens':face_tokens,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes,\n",
    "        'face_rectangle':face_rectangle,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max   \n",
    "    }\n",
    "    r_1 = requests.post(url,payload)\n",
    "    return r_1.json()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "523b1b71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3db3422f2af20f1d2b4987f921c5f083'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face01 = face_detect('jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf','N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR','https://img2.baidu.com/it/u=1460619778,3149038650&fm=253&fmt=auto&app=138&f=PNG?w=500&h=667')\n",
    "face01['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "93e3c81e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 55,\n",
       " 'request_id': '1647589109,3754258e-5a73-4f80-a61a-3904f0fb89ac',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 313, 'x': 228},\n",
       "    'mouth_upper_lip_top': {'y': 301, 'x': 244},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 302, 'x': 235},\n",
       "    'left_eye_upper_left_quarter': {'y': 237, 'x': 184},\n",
       "    'left_eyebrow_lower_middle': {'y': 211, 'x': 182},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 318, 'x': 238},\n",
       "    'right_eye_top': {'y': 195, 'x': 278},\n",
       "    'left_eye_bottom': {'y': 244, 'x': 197},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 177, 'x': 253},\n",
       "    'right_eye_pupil': {'y': 201, 'x': 281},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 307, 'x': 270},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 324, 'x': 271},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 314, 'x': 285},\n",
       "    'contour_chin': {'y': 374, 'x': 276},\n",
       "    'contour_left9': {'y': 372, 'x': 255},\n",
       "    'left_eye_lower_right_quarter': {'y': 239, 'x': 206},\n",
       "    'mouth_lower_lip_top': {'y': 314, 'x': 250},\n",
       "    'right_eyebrow_upper_middle': {'y': 156, 'x': 267},\n",
       "    'left_eyebrow_left_corner': {'y': 219, 'x': 162},\n",
       "    'right_eye_bottom': {'y': 209, 'x': 284},\n",
       "    'contour_left7': {'y': 351, 'x': 222},\n",
       "    'contour_left6': {'y': 336, 'x': 209},\n",
       "    'contour_left5': {'y': 320, 'x': 198},\n",
       "    'contour_left4': {'y': 303, 'x': 189},\n",
       "    'contour_left3': {'y': 285, 'x': 182},\n",
       "    'contour_left2': {'y': 266, 'x': 177},\n",
       "    'contour_left1': {'y': 247, 'x': 173},\n",
       "    'left_eye_lower_left_quarter': {'y': 246, 'x': 188},\n",
       "    'contour_right1': {'y': 181, 'x': 350},\n",
       "    'contour_right3': {'y': 229, 'x': 366},\n",
       "    'contour_right2': {'y': 204, 'x': 359},\n",
       "    'mouth_left_corner': {'y': 326, 'x': 230},\n",
       "    'contour_right4': {'y': 255, 'x': 369},\n",
       "    'contour_right7': {'y': 328, 'x': 346},\n",
       "    'right_eyebrow_left_corner': {'y': 187, 'x': 234},\n",
       "    'nose_right': {'y': 262, 'x': 267},\n",
       "    'nose_tip': {'y': 261, 'x': 231},\n",
       "    'contour_right5': {'y': 280, 'x': 367},\n",
       "    'nose_contour_lower_middle': {'y': 278, 'x': 241},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 214, 'x': 171},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 334, 'x': 244},\n",
       "    'right_eye_right_corner': {'y': 196, 'x': 300},\n",
       "    'right_eye_lower_right_quarter': {'y': 203, 'x': 293},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 296, 'x': 274},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 161, 'x': 290},\n",
       "    'left_eye_left_corner': {'y': 246, 'x': 180},\n",
       "    'mouth_right_corner': {'y': 303, 'x': 296},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 306, 'x': 270},\n",
       "    'right_eye_lower_left_quarter': {'y': 212, 'x': 271},\n",
       "    'left_eyebrow_right_corner': {'y': 204, 'x': 205},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 208, 'x': 194},\n",
       "    'right_eye_center': {'y': 203, 'x': 280},\n",
       "    'nose_left': {'y': 278, 'x': 221},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 319, 'x': 238},\n",
       "    'left_eye_upper_right_quarter': {'y': 229, 'x': 203},\n",
       "    'right_eyebrow_lower_middle': {'y': 165, 'x': 271},\n",
       "    'left_eye_top': {'y': 231, 'x': 192},\n",
       "    'left_eye_center': {'y': 238, 'x': 195},\n",
       "    'contour_left8': {'y': 363, 'x': 238},\n",
       "    'contour_right9': {'y': 364, 'x': 303},\n",
       "    'right_eye_left_corner': {'y': 213, 'x': 260},\n",
       "    'mouth_lower_lip_bottom': {'y': 331, 'x': 256},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 207, 'x': 167},\n",
       "    'left_eye_pupil': {'y': 235, 'x': 197},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 168, 'x': 247},\n",
       "    'contour_right8': {'y': 347, 'x': 326},\n",
       "    'right_eyebrow_right_corner': {'y': 166, 'x': 312},\n",
       "    'right_eye_upper_left_quarter': {'y': 202, 'x': 267},\n",
       "    'left_eyebrow_upper_middle': {'y': 200, 'x': 178},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 155, 'x': 291},\n",
       "    'nose_contour_left1': {'y': 228, 'x': 218},\n",
       "    'nose_contour_left2': {'y': 261, 'x': 219},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 295, 'x': 252},\n",
       "    'nose_contour_right1': {'y': 218, 'x': 243},\n",
       "    'nose_contour_right2': {'y': 248, 'x': 256},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 331, 'x': 235},\n",
       "    'contour_right6': {'y': 305, 'x': 360},\n",
       "    'nose_contour_right3': {'y': 271, 'x': 255},\n",
       "    'nose_contour_left3': {'y': 280, 'x': 231},\n",
       "    'left_eye_right_corner': {'y': 233, 'x': 213},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 198, 'x': 192},\n",
       "    'right_eye_upper_right_quarter': {'y': 193, 'x': 289},\n",
       "    'mouth_upper_lip_bottom': {'y': 313, 'x': 249}},\n",
       "   'face_rectangle': {'width': 206, 'top': 159, 'left': 175, 'height': 206},\n",
       "   'face_token': '3db3422f2af20f1d2b4987f921c5f083'}]}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_analyze('jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf','N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR',face01['faces'][0]['face_token'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c745633",
   "metadata": {},
   "source": [
    "###  Python函数的方法，封装face++ 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f83d6c8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'376628333aa16867f67e0247d142c918'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face03 = face_detect('jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf','N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR','https://img2.baidu.com/it/u=1460619778,3149038650&fm=253&fmt=auto&app=138&f=PNG?w=500&h=667')\n",
    "face03['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "71ed6b75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3d034e92c35d7005f938eb288630be8e'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face04 = face_detect('jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf','N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR','https://img0.baidu.com/it/u=3882575275,2540357453&fm=253&fmt=auto&app=138&f=JPEG?w=401&h=500')\n",
    "face04['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "44e6b070",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_compare(API_key,API_secret,face_token1,face_token2):\n",
    "    \"\"\"face++ 人脸对比接口函数(图片链接)\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "    payload={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'face_token1':face_token1,\n",
    "        'face_token2':face_token2  \n",
    "    }\n",
    "    r_2 = requests.post(url,payload)\n",
    "    return r_2.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "044bc9d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647590026,36e907ad-2b7d-4f8e-ac0f-3295883f2000',\n",
       " 'time_used': 370,\n",
       " 'confidence': 89.127,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_compare('jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf','N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR',face03['faces'][0]['face_token'],face04['faces'][0]['face_token'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c693380b",
   "metadata": {},
   "source": [
    "## 请同学们调用face++ 人脸搜索API接口和人脸库API组\n",
    "### face++人脸库API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "c5d01854",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建人脸库API\n",
    "# 1.url\n",
    "create_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf'\n",
    "API_sercret = 'N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'display_name':\"Kylie Jenner\",\n",
    "    'face_tokens':'3d034e92c35d7005f938eb288630be8e,376628333aa16867f67e0247d142c918,3db3422f2af20f1d2b4987f921c5f083,ef118926843f1fff2a46b5099c3ab40c'\n",
    "    \n",
    "    \n",
    "    \n",
    "}\n",
    "r = requests.post(create_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "06a4ba23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "eaacb46b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6d4a02650435e771a75555d4b7040259',\n",
       " 'time_used': 1020,\n",
       " 'face_count': 4,\n",
       " 'face_added': 4,\n",
       " 'request_id': '1647592113,4d37e3a3-9b1d-4483-a635-3f7f3a18ae5d',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a13b1a4",
   "metadata": {},
   "source": [
    "### face++人脸搜索API接口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "0722e4a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "search_url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'jVvZmc_3Fnub6PS-UCZJovCn21Rd-BUf'\n",
    "API_sercret = 'N2kKnsLpbE6vCY81kmcr3fL5ZFgRPtOR'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://img0.baidu.com/it/u=3185430162,4001561422&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=566',\n",
    "    'faceset_token': '6d4a02650435e771a75555d4b7040259'\n",
    "    \n",
    "    \n",
    "}\n",
    "r = requests.post(search_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "d66c233d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "f1735606",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'image_id': 'F4+8xvqbJ03caO//T/PvFQ==',\n",
       " 'faces': [{'face_rectangle': {'width': 181,\n",
       "    'top': 169,\n",
       "    'left': 160,\n",
       "    'height': 181},\n",
       "   'face_token': '3882bb77a1fa30e8b9200808420069ed'}],\n",
       " 'time_used': 518,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'request_id': '1647592293,85962e87-ff20-4be7-89d0-f7f88c4ea7d9',\n",
       " 'results': [{'confidence': 92.32,\n",
       "   'user_id': '',\n",
       "   'face_token': 'ef118926843f1fff2a46b5099c3ab40c'}]}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "847a20e4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
